// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/efficiency/api/EnergyQuotaApi'

import ListClass from '@api/efficiency/model/EnergyQuotaStatisticReq'

const CLIENT = CORE.grpcCLIENT(Client)

const PLAN = {
  1: 'electricPlanQuota',
  2: 'waterPlanQuota',
  3: 'gasPlanQuota',
}
const USE = {
  1: 'electricUseQuota',
  2: 'waterUseQuota',
  3: 'gasUseQuota',
}

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param)
    const resp = await CORE.grpc(TIP, CLIENT, 'energyQuotaStatistic', DTO)
    return [1, resp.map]
  },
  transList({ 整体定额: all, ...areaObj }, { state: { DICT, param } }) {
    const transXData = (arr) => (arr || []).map(({ xaxis }) => xaxis[0] || '-')
    const transDataList = (arr, key) => arr.map((item) => item[key][0] || 0)

    if (param.quotaCategory === 1) {
      const xData = transXData(all)
      return DICT.TYPE.map(([type, name, code, unit]) => ({
        title: `${name}定额执行情况`,
        unit,
        xData,
        dataList: [transDataList(all, PLAN[type]), transDataList(all, USE[type])],
      }))
    }

    return Object.keys(areaObj).map((title) => {
      const arr = areaObj[title]
      return {
        title,
        unit: CORE.dict(DICT.TYPE, param.energyType, 3),
        xData: transXData(arr),
        dataList: [transDataList(arr, PLAN[param.energyType]), transDataList(arr, USE[param.energyType])],
      }
    })
  },
}
